package com.cus.tom.transfer.db;

import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cus.tom.transfer.bean.FileType;
import com.cus.tom.transfer.bean.MediaData;
import com.cus.tom.transfer.bean.TypeFile;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public abstract class TransDao extends BaseDao {

    abstract protected String getTableName();

    public void delete(final long id) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            int result = db.delete(getTableName(), "id=?", new String[]{String.valueOf(id)});
            System.currentTimeMillis();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void delete(List<Long> id) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            for (long l : id) {
                int result = db.delete(getTableName(), "id=?", new String[]{String.valueOf(l)});
            }
            System.currentTimeMillis();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public TypeFile insert(final TypeFile bean, String flag) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return bean;
            }
            ContentValues values = new ContentValues();
            values.put("path", bean.path);
            values.put("type", bean.type.getCode());
            values.put("time", System.currentTimeMillis());
            values.put("flag", flag);
            bean.id = db.insertWithOnConflict(getTableName(), null, values, CONFLICT_NONE);
            return bean;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return bean;
    }

    public void getAll(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                SQLiteDatabase db = null;
                Cursor cursor = null;
                try {
                    db = getDataBase();
                    if (db == null) {
                        if (listener != null) {
                            listener.onResult(false, "打开数据库失败", Collections.emptyList());
                        }
                        return;
                    }
                    cursor = db.rawQuery("select * from " + getTableName() + " order by time desc", null);
                    if (cursor == null || cursor.getCount() <= 0) {
                        if (listener != null) {
                            listener.onResult(true, null, Collections.emptyList());
                        }
                        return;
                    }
                    List<TypeFile> list = new LinkedList<>();
                    while (cursor.moveToNext()) {
                        TypeFile item = new TypeFile();
                        item.path = cursor.getString(cursor.getColumnIndexOrThrow("path"));
                        int i = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                        item.type = FileType.valueOf(i);
                        item.id = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                        item.time = cursor.getLong(cursor.getColumnIndexOrThrow("time"));
                        list.add(item);
                    }
                    if (listener != null) {
                        listener.onResult(true, null, list);
                    }
                } catch (Throwable e) {
                    if (listener != null) {
                        listener.onResult(false, e.toString(), Collections.emptyList());
                    }
                } finally {
                    closeCursor(cursor);
                    if (db != null) {
                        closeDataBase();
                    }
                }
            }
        });
    }

    public List<TypeFile> getAllById(String id) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return Collections.emptyList();
            }
            cursor = db.rawQuery("select * from " + getTableName() + " where flag='" + id + "' order by time desc", null);
            if (cursor == null || cursor.getCount() <= 0) {
                return Collections.emptyList();
            }
            List<TypeFile> list = new LinkedList<>();
            while (cursor.moveToNext()) {
                TypeFile item = new TypeFile();
                item.path = cursor.getString(cursor.getColumnIndexOrThrow("path"));
                int i = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                item.type = FileType.valueOf(i);
                item.id = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                item.time = cursor.getLong(cursor.getColumnIndexOrThrow("time"));
                list.add(item);
            }
            return list;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
        return Collections.emptyList();
    }

    public List<MediaData> getAllByType(FileType type) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return Collections.emptyList();
            }
            String sql = "select * from " + getTableName() + " where type=" + type.getCode() + " order by time desc";
            cursor = db.rawQuery(sql, null);
            if (cursor == null || cursor.getCount() <= 0) {
                return Collections.emptyList();
            }
            List<MediaData> list = new LinkedList<>();
            while (cursor.moveToNext()) {
                MediaData item = new MediaData();
                item.setType(type.getCode());
                String path = cursor.getString(cursor.getColumnIndexOrThrow("path"));
                item.setPath(path);
                item.id = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
                long time = cursor.getLong(cursor.getColumnIndexOrThrow("time"));
                item.setAddTime(time / 1000);
                item.setName(new File(path).getName());
                list.add(item);
            }
            return list;
        } catch (Throwable e) {
            e.printStackTrace();
            return Collections.emptyList();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }
}
